layui.define(['jquery', 'layer', 'laypage'], function(exports) {
    "use strict";
    var MOD_NAME = 'fs';
    var $ = layui.jquery,
        layer = layui.layer,
        laypage = layui.laypage;


    /**
     * 前端文件略缩图展示
     * 多个文件后缀对应一种略缩图展示
     * @param fileType 文件类型
     */
    function fileThumbnail(fileType){

        let thumbnail = {};

        if (null == fileType){
            return "folder.png";
        }

        //判断是什么类型的文件
        switch (fileType) {
            case 1:
                thumbnail.url = "/admin/images/file_icon/normal.png"
                break;
            case 2:
                thumbnail.url = "/admin/images/file_icon/zip.png"
                thumbnail.suffix = [".zip"];
                break;
            case 3:
                thumbnail.url = "/admin/images/file_icon/excel.png"
                thumbnail.suffix =  [".xlsx", ".xls"];
                break;
            case 4:
                thumbnail.url = "/admin/images/file_icon/word.png"
                thumbnail.suffix =  [".docx", ".doc"];
                break;
            case 5:
                thumbnail.url = "/admin/images/file_icon/pdf.png"
                thumbnail.suffix =  [".pdf"];
                break;
            case 6:
                thumbnail.url = "/admin/images/file_icon/txt.png"
                thumbnail.suffix =  [".txt"];
                break;
            case 8:
                thumbnail.url = "/admin/images/file_icon/music.png"
                thumbnail.suffix =  [".mp3", ".mkv", ".mpg", ".rm", ".wma"];
                break;
            case 9:
                thumbnail.url = "/admin/images/file_icon/video.png"
                thumbnail.suffix =  [".avi", ".3gp", ".mp4", ".flv", ".rmvb", ".mov",".mkv"];
                break;
            case 10:
                thumbnail.url = "/admin/images/file_icon/ppt.png"
                thumbnail.suffix =  [".ppt"];
                break;
            case 11:
                thumbnail.url = "/admin/images/file_icon/code.png"
                thumbnail.suffix = [".java", ".obj", ".h", ".c", ".html",".htm", ".net", ".php", ".css", ".js", ".ftl", ".jsp", ".asp",".vue"];
                break;
            case 12:
                thumbnail.url = "/admin/images/file_icon/other.png"
                thumbnail.suffix = [".ps",".psd",".cad"];
                break;
            case 13:
                thumbnail.url = "/admin/images/file_icon/exe.png"
                thumbnail.suffix = [".exe",".bat",".sh"];
                break;
            case 14:
                thumbnail.url = "/admin/images/file_icon/bt.png"
                thumbnail.suffix = [".bt"];
                break;
        }
        return thumbnail;
    }

    /**
     * 接收一个参数 当前上传的文件
     * @param file
     */
    function getBlobUrl(file){

        let reader = new FileReader(file)

        //参数是选择的文件
        reader.readAsArrayBuffer(file)
        reader.onload = (e) =>{
            let data ;
            if (typeof e.target.result === 'object'){
                data = window.URL.createObjectURL(new Blob([e.target.result]))
            }
            else{
                data = e.target.result
            }
            console.log("视频地址是："+data)
        }
    }

    /**
     * 需要导出的对象
     * @type {{getFileSuffixByFileType: (function(*): string|{})}}
     */
    var util = {
        /**
         * 文件类型
         * @param fileType
         * @returns {string|{}}
         */
        getFileSuffixByFileType:function (fileType){
            return  fileThumbnail(fileType);
        },
        getBlobUrl:function (file){
            return getBlobUrl(file);
        },
        getFileSizeDesc:function (bytes){
            let result = {}
            if (bytes < 1024) {
                result.num = parseFloat(bytes)
                result.unit = 'bytes'
                return result;
            } else if (bytes < 1024 * 1024) {
                result.num = parseFloat((bytes / 1024).toFixed(2))
                result.unit = 'KB'
                return  result;
            } else if (bytes < 1024 * 1024 * 1024) {
                result.num = parseFloat((bytes / (1024 * 1024)).toFixed(2))
                result.unit = 'MB'
                return result;
            } else {
                result.num = parseFloat((bytes / (1024 * 1024 * 1024)).toFixed(2))
                result.unit = 'GB'
                return result;
            }
        }

    }
    exports(MOD_NAME, util);
});